package com.lc.user.express.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.GetOrderCancel;
import com.lc.user.express.httpserver.GetOrderConfirm;
import com.lc.user.express.httpserver.GetOrderDelete;
import com.lc.user.express.httpserver.GetOrderPay;
import com.lc.user.express.httpserver.GetOrderQueRen;
import com.lc.user.express.model.OrderModel;
import com.lc.user.express.ordering.OrderInfoCompleteActivity;
import com.lc.user.express.ordermanagement.CommentActivity;
import com.lc.user.express.ordermanagement.OrderManagementFragment;
import com.lc.user.express.utils.GetToast;
import com.lc.user.express.utils.PublicMethod;
import com.lc.user.express.view.dialog.AffirmDialog;
import com.zcx.helper.http.AsyCallBack;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2016/3/2.
 */
public class OrderAdapter extends BaseListAdapter<OrderModel> {
    public OrderAdapter(Context context, List<OrderModel> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        OrderModel model = data.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_list, null);
            AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
            viewHolder = new ViewHolder();
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_status = (TextView) convertView.findViewById(R.id.tv_status);
            viewHolder.ll_driver = (LinearLayout) convertView.findViewById(R.id.ll_driver);
            viewHolder.tv_driver_name = (TextView) convertView.findViewById(R.id.tv_driver_name);
            viewHolder.tv_start = (TextView) convertView.findViewById(R.id.tv_start);
            viewHolder.tv_end = (TextView) convertView.findViewById(R.id.tv_end);
            viewHolder.tv_fee = (TextView) convertView.findViewById(R.id.tv_fee);
            viewHolder.tv_order_do = (TextView) convertView.findViewById(R.id.tv_order_do);
            viewHolder.iv_driver_head = (SimpleDraweeView) convertView.findViewById(R.id.iv_driver_head);
            viewHolder.iv_call = (ImageView) convertView.findViewById(R.id.iv_call);
            viewHolder.tv_disc = (TextView) convertView.findViewById(R.id.tv_disc);
            viewHolder.tv_order_querenfahuo = (TextView) convertView.findViewById(R.id.tv_order_queren);
            convertView.setTag(viewHolder);
        }
        setContentView(convertView, model);
        return convertView;
    }

    private void setContentView(final View contentView, final OrderModel model) {
        final ViewHolder viewHolder = (ViewHolder) contentView.getTag();
        viewHolder.tv_time.setText(model.getoDatetype() + " " + model.getoTime());
        viewHolder.tv_driver_name.setText(model.getoDriver());
        viewHolder.tv_start.setText(model.getoStart());
        viewHolder.tv_end.setText(model.getoEnd());
        viewHolder.tv_fee.setText("费用: ￥" + model.getoFee());
        Log.d(model.getoStatus()+"","0000000000000000000");



//        ImageManager.from(mContext).displayImage(viewHolder.iv_driver_head, model.getoDriverHead(), R.mipmap.default_pic);

     /*   Glide.with(mContext)
                .load(model.getoDriverHead())
                .placeholder(R.drawable.shape_background) // can also be a drawable
                .error(R.mipmap.default_pic ) // will be displayed if the image cannot be loaded
                .crossFade()
                .into(viewHolder.iv_driver_head);*/

        viewHolder.iv_driver_head.setImageURI(Uri.parse(model.getoDriverHead()));

        Log.d(model.getoDriverHead(),"+++++++++++++++++++++++++");
        int size = model.getAddrlist().size();
        if (size > 1) {
            viewHolder.tv_start.setText(model.getAddrlist().get(0).getAddr());
            viewHolder.tv_end.setText(model.getAddrlist().get(size - 1).getAddr());
        } else {
            viewHolder.tv_start.setText("");
            viewHolder.tv_end.setText("");
        }

        switch (model.getoStatus()) {

            case OrderModel.STATUS_REC_ORDER:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.green));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.VISIBLE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.tv_status.setText("已接单");
                viewHolder.tv_order_do.setText("取消订单");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.VISIBLE);
                break;
            case OrderModel.STATUS_RECEIVE_GOODS:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.GONE);
                viewHolder.tv_status.setText("已收货");
                viewHolder.tv_order_do.setText("确认付款");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);

                break;
            case OrderModel.STATUS_PAY:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.blue2));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.GONE);
                viewHolder.tv_status.setText("已付款");
//                viewHolder.tv_order_do.setText("评价");
                viewHolder.tv_order_do.setVisibility(View.GONE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);


                break;
            case OrderModel.STATUS_CANCEL:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.gray1));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.gray));
                viewHolder.tv_status.setText("已取消");
                viewHolder.tv_order_do.setText("再次下单");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.GONE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.GONE);

                break;
            case OrderModel.STATUS_SENDING_GOODS:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.orange));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.VISIBLE);
                viewHolder.tv_status.setText("送货中");
                viewHolder.tv_order_do.setText("确认收货");
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.tv_order_do.setVisibility(View.GONE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.GONE);

                break;
            case OrderModel.STATUS_SENDING_ORDER:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.green1));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.tv_status.setText("派单中");
                viewHolder.tv_order_do.setText("取消订单");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.tv_driver_name.setText("稍后显示服务司机");
                viewHolder.tv_disc.setVisibility(View.GONE);
                viewHolder.iv_call.setVisibility(View.GONE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.GONE);

                break;
            case OrderModel.STATUS_ARRIVED:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.blue3));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.VISIBLE);
                viewHolder.tv_status.setText("已到达");
                // viewHolder.tv_order_do.setText("确认收货");
                viewHolder.tv_order_do.setVisibility(View.GONE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.tv_order_do.setText("去评价");
                viewHolder.ll_driver.setVisibility(View.VISIBLE);

                break;
            case OrderModel.STATUS_FINISHED:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.blue4));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.GONE);
                viewHolder.tv_status.setText("已完成");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.tv_order_do.setText("评价");
                viewHolder.tv_order_do.setVisibility(View.VISIBLE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.GONE);

                break;
            case OrderModel.STATUS_COMMENTED:
                viewHolder.tv_status.setBackgroundColor(mContext.getResources().getColor(R.color.blue5));
                viewHolder.tv_status.setTextColor(mContext.getResources().getColor(R.color.white));
                viewHolder.iv_call.setVisibility(View.GONE);
                viewHolder.tv_status.setText("已评价");
                viewHolder.tv_order_do.setVisibility(View.GONE);
                viewHolder.tv_disc.setVisibility(View.VISIBLE);
                viewHolder.ll_driver.setVisibility(View.VISIBLE);
                viewHolder.tv_order_querenfahuo.setVisibility(View.GONE);
                break;
        }
        viewHolder.tv_order_do.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (model.getoStatus()) {
                    case OrderModel.STATUS_RECEIVE_GOODS:
                        orderPay(model.getoId());
                        break;
                    case OrderModel.STATUS_CANCEL:
                        Intent i = new Intent(mContext, OrderInfoCompleteActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("addrs", (Serializable) model.getAddrlist());
//                        bundle.putString("name1", model.getAddrlist().get(0).getName());
//                        bundle.putString("name2",model.getAddrlist().get(1).getName());
                        bundle.putString("cid", model.getoCid());
                        bundle.putString("car_name", model.getoCname());
                        i.putExtras(bundle);
                        i.addFlags(OrderInfoCompleteActivity.TYPE_REORDER);
                        mContext.startActivity(i);
                        break;
                    case OrderModel.STATUS_SENDING_GOODS:
                    case OrderModel.STATUS_ARRIVED:
                        orderConfirm(model.getoId());
                        break;
                    case OrderModel.STATUS_REC_ORDER:
                    case OrderModel.STATUS_SENDING_ORDER:
                        showCancelDialog(model.getoId());
                        Log.d(model.getoId(),"==============");

                        break;
                    case OrderModel.STATUS_FINISHED:
                        Intent intent = new Intent(mContext, CommentActivity.class);
                        intent.putExtra("orderid", model.getoId());
                        mContext.startActivity(intent);
                        break;

                }
            }
        });

        viewHolder.iv_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(model.getoMobile())) {
                    GetToast.useString(mContext, "暂无电话号码");
                } else {
                    PublicMethod.callPhone(mContext, model.getoMobile());
                }

            }
        });

        viewHolder.tv_order_querenfahuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                orderQueRen(model.getoId());
                Toast.makeText(mContext, "已确认发货", Toast.LENGTH_SHORT).show();

        /*
        确认发货
         */
            }
        });
    }

    private void showCancelDialog(final String orderid) {
        new AffirmDialog(mContext, "您确定要取消该订单吗？", "取消", "确定", R.style.MyDialog) {

            @Override
            protected void onYesClick() {

                // TODO Auto-generated method stub
                cancelOrder(orderid);
            }
        }.show();
    }


    private void cancelOrder(String orderid) {
        new GetOrderCancel(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(mContext, toast);
            }
        }).execute(mContext);//调接口
    }


    private void orderConfirm(String orderid) {
        new GetOrderConfirm(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(mContext, toast);
            }
        }).execute(mContext);//调接口
    }

    private void orderPay(String orderid) {
        new GetOrderPay(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(mContext, toast);
            }
        }).execute(mContext);//调接口
    }

    private void orderQueRen(String orderid) {
        new GetOrderQueRen(orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);
                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();
                }
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);

                if (!TextUtils.isEmpty(toast)) GetToast.useString(mContext, toast);
            }
        }).execute(mContext);//调接口
    }

    private void deleteOrder(String orderid) {
        new GetOrderDelete(AppContext.myPreferences.getUid(), orderid, new AsyCallBack<Object>() {
            @Override
            public void onSuccess(String toast, int type, Object info) throws Exception {
                super.onSuccess(toast, type, info);

                if (OrderManagementFragment.onRefreshOrderData != null) {
                    OrderManagementFragment.onRefreshOrderData.onRefresh();


                }

            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                if (!TextUtils.isEmpty(toast)) GetToast.useString(mContext, toast);
            }
        }).execute(mContext);//调接口
    }

    private class ViewHolder {
        TextView tv_time;//时间
        TextView tv_status;//状态
        LinearLayout ll_driver;//司机
        TextView tv_driver_name;//司机姓名
        SimpleDraweeView iv_driver_head;//司机头像
        ImageView iv_call;//打电话
        TextView tv_start;//发货地
        TextView tv_end;//终点
        TextView tv_fee;//费用
        TextView tv_order_do;//操作按钮
        TextView tv_disc;//描述 为人民服务
        TextView tv_order_querenfahuo;
    }
}
